/**
 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xywy.wjk.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.util.ArrayList;
import java.util.List;

public class WjkChart extends AbstractChart {
  /**
   * Returns the chart name.
   * 
   * @return the chart name
   */
  public String getName() {
    return "Average temperature";
  }

  /**
   * Returns the chart description.
   * 
   * @return the chart description
   */
  public String getDesc() {
    return "The average temperature in 4 Greek islands (line chart)";
  }


    public GraphicalView CreateView(Context context){
        String[] titles = new String[] { "COD达标率 ", "氨氮达标率"};
        // x轴的值
        List<double[]> x = new ArrayList<double[]>();
        for (int i = 0; i < titles.length; i++) {
            x.add(new double[] {1,2,3,4,5,6,7,8,9});   //从X周的 0算起来 总共有几个点
        }
        // y轴的值
        List<double[]> values = new ArrayList<double[]>();

        values.add(new double[] { 10.3, 12.5, 13.8, 24.5, 30.4, 40.4, 50.4, 55.5, 60.6});
        values.add(new double[] { 10, 15.6, 20.5, 18.6, 25.7, 30.6, 44.5, 55.5 ,64.8});

        int[] colors = new int[] { Color.BLUE, Color.GREEN};
        PointStyle[] styles = new PointStyle[] { PointStyle.POINT,PointStyle.POINT };

        XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
            ((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
        }
        renderer.setBackgroundColor(Color.WHITE);
        renderer.setApplyBackgroundColor(true);
        renderer.setFitLegend(true);
        // 图表与屏幕四边的间距颜色
        renderer.setMarginsColor(Color.argb(0, 0xF3, 0xF3, 0xF3));
        renderer.setChartTitleTextSize(30);
        renderer.setAxisTitleTextSize(10);
        // renderer.setLegendHeight(50);
        renderer.setLegendTextSize(10);                    // 图例文字的大小--下面分类的字体大小
        renderer.setMargins(new int[] { 50, 50, 20, 30 }); // 图离上做下右的间距,貌似没用
        // x、y轴上刻度颜色
        renderer.setXLabelsColor(Color.BLACK);
        renderer.setYLabelsColor(0, Color.BLACK);

        setChartSettings(renderer, "", "","", 1, 9 , 0, 90, Color.BLACK, Color.BLACK);

        // 轴上数字的数量
        renderer.setXLabels(0);
        renderer.setYLabels(9);

        /**
         *  本项目 -  传入 第一个点 为 X轴的0坐标 为上一天的点数，所以不能为0 ，    第2个点位当天点数
         */
        renderer.addXTextLabel(2, "6月1号");
        renderer.addXTextLabel(3, "6月2号");
        renderer.addXTextLabel(4, "6月3号");
        renderer.addXTextLabel(5, "6月4号");
        renderer.addXTextLabel(6, "6月5号");
        renderer.addXTextLabel(7, "6月6号");
        renderer.addXTextLabel(8, "6月7号");
        renderer.addXTextLabel(9, "6月8号");

        renderer.setShowGrid(true);    // 是否显示网格
        renderer.setXLabelsAlign( Align.RIGHT);
        renderer.setYLabelsAlign(Align.RIGHT);

        renderer.setPanEnabled(false);   //图表是否可以移动
        renderer.setZoomEnabled(true);   //图表是否可以缩放
        renderer.setLegendHeight(100);   // 图标文字距离底边的高度
        renderer.setZoomButtonsVisible(false);   // 设置放大缩小按钮是否可见
        GraphicalView view= ChartFactory.getLineChartView(context, buildDataset(titles, x, values), renderer);
        return view;  //返回View对象
    }
    /**
         * Executes the chart demo.
         *
         * @param context the context
         * @return the built intent
         */
  public Intent execute(Context context) {
      // 每个item的title
      String[] titles = new String[] { "Crete", "Corfu", "Thassos",
              "Skiathos" };
      // x轴的值
      List<double[]> x = new ArrayList<double[]>();
      for (int i = 0; i < titles.length; i++) {
          x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
      }
      // y轴的值
      List<double[]> values = new ArrayList<double[]>();
      values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4,
              26.1, 23.6, 20.3, 17.2, 13.9 });
      values.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14,
              11 });
      values.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 });
      values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });
      int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN,
              Color.YELLOW };
      // 点的样式
      PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE,
              PointStyle.DIAMOND, PointStyle.TRIANGLE, PointStyle.SQUARE };
      XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
      int length = renderer.getSeriesRendererCount();
      // 点是空心还是实心
      for (int i = 0; i < length; i++) {
          ((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
                  .setFillPoints(true);
      }

      // 图表部分的背景颜色
      renderer.setBackgroundColor(Color.parseColor("#f3f3f3"));
      renderer.setApplyBackgroundColor(true);
      // 图表与屏幕四边的间距颜色
      renderer.setMarginsColor(Color.argb(0, 0xF3, 0xF3, 0xF3));
      renderer.setChartTitleTextSize(30);
      renderer.setAxisTitleTextSize(25);
      // renderer.setLegendHeight(50);
      // 图例文字的大小
      renderer.setLegendTextSize(20);
      renderer.setMargins(new int[] { 50, 50, 50, 30 });
      // x、y轴上刻度颜色
      renderer.setXLabelsColor(Color.BLACK);
      renderer.setYLabelsColor(0, Color.BLACK);

      // 最后两个参数代表轴的颜色和轴标签的颜色
      setChartSettings(renderer, "Average temperature", "Month",
              "Temperature", 0.5, 12.5, -10, 40, Color.BLACK, Color.BLACK);
      // 轴上数字的数量
      renderer.setXLabels(12);
      renderer.setYLabels(10);
      // 是否显示网格
      renderer.setShowGrid(true);
      // x或y轴上数字的方向，相反的。
      renderer.setXLabelsAlign(Align.RIGHT);
      renderer.setYLabelsAlign(Align.RIGHT);



      Intent intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, x, values), renderer,"Average temperature");

      return intent;
  }

}
